package com.itheima.project.project;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;
import com.itheima.project.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class FragmentProject extends Fragment {

    private SlidingTabLayout stlTitle;
    private ViewPager vpContent;
    private List<FragmentSencondProject> mList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.project_fragment, null, false);
        stlTitle = inflate.findViewById(R.id.stl_title_project_fragment);
        vpContent = inflate.findViewById(R.id.vp_content_project_fragment);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request builder = new Request.Builder()
                .url("https://www.wanandroid.com/project/tree/json")
                .build();
        Call call = okHttpClient.newCall(builder);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                SlidingTabLayoutEntity slidingTabLayoutEntity = new Gson().fromJson(string, SlidingTabLayoutEntity.class);
                if (slidingTabLayoutEntity.getErrorCode() ==0) {
                    mList.clear();
                    final List<SlidingTabLayoutEntity.DataBean> data = slidingTabLayoutEntity.getData();
                    for(int i=0;i<data.size();i++){
                        SlidingTabLayoutEntity.DataBean dataBean = data.get(i);
                        FragmentSencondProject instance = FragmentSencondProject.Instance(dataBean.getId());
                        mList.add(instance);
                    }
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            vpContent.setAdapter(new FragmentStatePagerAdapter(getFragmentManager()) {
                                @Override
                                public Fragment getItem(int position) {
                                    return mList.get(position);
                                }
                                @Override
                                public int getCount() {
                                    return mList == null? 0 : mList.size();
                                }
                                @Override
                                public CharSequence getPageTitle(int position) {
                                    return data.get(position).getName();
                                }
                            });
                            stlTitle.setViewPager(vpContent);
                        }
                    });


                }

            }
        });
    }

}
